package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.gusha.dao.ConfigAgentDao;
import com.gusha.model.ConfigAgent;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:ConfigAgentService服务类
 */

@Service
public class ConfigAgentService {

    @Autowired
    private ConfigAgentDao configAgentDao;

    public Page<ConfigAgent> findAll(Pageable pageable, ConfigAgent configAgent) {
        @SuppressWarnings("serial")
        Page<ConfigAgent> result = configAgentDao.findAll(new Specification<ConfigAgent>() {
            @Override
            public Predicate toPredicate(Root<ConfigAgent> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (configAgent.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),configAgent.getId()));
                }
                if (StringUtils.isNotBlank(configAgent.getTitle())) {
                    list.add(cb.equal(root.get("title").as(String.class),configAgent.getTitle()));
                }
                if (configAgent.getManNumDownline()!=null) {
                    list.add(cb.equal(root.get("manNumDownline").as(BigDecimal.class),configAgent.getManNumDownline()));
                }
                if (configAgent.getBusinessNumDownline()!=null) {
                    list.add(cb.equal(root.get("businessNumDownline").as(BigDecimal.class),configAgent.getBusinessNumDownline()));
                }
                if (configAgent.getBusinessNumAvg()!=null) {
                    list.add(cb.equal(root.get("businessNumAvg").as(BigDecimal.class),configAgent.getBusinessNumAvg()));
                }
                if (configAgent.getAchievement()!=null) {
                    list.add(cb.equal(root.get("achievement").as(BigDecimal.class),configAgent.getAchievement()));
                }
                if (configAgent.getAllowance()!=null) {
                    list.add(cb.equal(root.get("allowance").as(BigDecimal.class),configAgent.getAllowance()));
                }
                if (configAgent.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),configAgent.getCreateTime()));
                }
                if (configAgent.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),configAgent.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(configAgent.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),configAgent.getCreator()));
                }
                if (StringUtils.isNotBlank(configAgent.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),configAgent.getUpdator()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
